import java.lang.reflect.Array;
import java.util.Arrays;
class Testdemo {
    public String name;
    public int age ;
    public static int count;
    public static void change() {
        count = 10;                 // 可以访问静态数据成员，并可以修改
    }
}
public class Demo {
    public static void main(String[] args) {
        Testdemo.change();        // 无需创建对象，可通过类直接调用静态方法
        System.out.println(Testdemo.count);
    }
    // 打印二维数组
    public static void print(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }
    public static void main7(String[] args) {
        int[][] arr= {{1,2,3},{4,5,6},};
        print(arr);
        System.out.println("===========");
        System.out.println(Arrays.deepToString(arr));
    }
    // 数组拷贝
    public static void main6(String[] args) {
        int[] arr = {1,2,3,4,5};
        int[] newarr = Arrays.copyOf(arr,arr.length);      // 第一种拷贝方式，参数：前面是哪个数组，后面是新数组的长度
        System.out.println(Arrays.toString(newarr));
        newarr[0] = 0;
        System.out.println(Arrays.toString(arr));          //
        System.out.println(Arrays.toString(newarr));
        int[] newarr2 = Arrays.copyOfRange(arr,2,4);     // 第二种拷贝方式，参数：前面数组，后面是范围是左闭右开
        System.out.println(Arrays.toString(newarr2));
    }
    // 给定一个数组, 将里面的元素逆序排列
    public static void reverse(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while (left < right) {
            int temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
    }
    public static void main5(String[] args) {
        int[] arr = {1,2,3,4,5};
        reverse(arr);
        System.out.println(Arrays.toString(arr));
    }
    // 给定一个数组, 让数组升序 (降序) 排序(冒泡排序)
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            boolean fal = true;
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    fal = false;
                }
            }
            if (fal == true) {
                break;
            }
        }
    }
    public static void main4(String[] args) {
        int[] arr = {1,4,3,5,2};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    // 给定一个整型数组, 判断是否该数组是有序的(升序)
    public static boolean isup (int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            if (arr[i]>arr[i+1]) {
                return false;
            }
        }
        return true;
    }
    public static void main3(String[] args) {
        int[] arr = {1,2,3,4,5};
        System.out.println(isup(arr));
    }
    // 给定一个数组, 再给定一个元素, 找出该元素在数组中的位置(二分查找)
    public static int binarySerch (int[] arr, int n) {
        int left = 0;
        int right = arr.length-1;
        int mid = (left + right)/2;
        while (arr[mid] != n) {
            if (arr[mid] < n) {
                left = mid + 1;
            }else if (arr[mid] > n) {
                right = mid - 1;
            }
            mid = (left + right)/2;
        }
        return mid+1;
    }
    public static void main2(String[] args) {
        int[] arr = {1,2,3,4,5};
        int n = 1;
        System.out.println(binarySerch(arr,n));
    }
    // 给定一个数组, 再给定一个元素, 找出该元素在数组中的位置
    public static int findNum (int[] arr, int n) {
        for (int i = 0; i < arr.length ; i++) {
            if (arr[i] == n) {
                return  i+1;
            }
        }
        return -1;
    }
    public static void main1(String[] args) {
        int[] arr = {1,2,3,4,5};
        int n = 3;
        System.out.println(findNum(arr,3));
    }
}
